﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using dhDataAcess.dataAcess.stockDataSetTableAdapters;
using dhDataAcess.dataAcess.produceDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Windows.Forms;
using baseSet.importExport;
using dhSystemBLL.produceBLL;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;
using baseSet;

namespace dhsystem.produceForm.Stock
{
    public partial class FrmpackCount : Form
    {
        public string pname { get; set; }
        public string boxnoI { get; set; }
        public string MweightI { get; set; }
        public string JweightI { get; set; }
        public string unitI { get; set; }
        public string qtyI { get; set; }
        public string pNameI { get; set; }
        public produceDataSet.orderStockOutDetailDataTable  pTableI { get; set; }
        string tablename;
        string field;
 
        string boxno;
        string Mweight;
        string Jweight;
        string unit;
        string qty;
        string pName;
        string billNo;
   
        custListBLL custList = new custListBLL();
        packStockInBLL packStockIn = new packStockInBLL();
        public FrmpackCount(string _tablenam, string _field, string _boxno, string _Mweight, string _Jweight, string _unit, string   _qty, string _pName,string _billNo)
        {
            InitializeComponent();
            tablename = _tablenam;
            field = _field;
            boxno =_boxno;
            Mweight = _Mweight;
            Jweight = _Jweight;
            unit = _unit;
            qty =_qty ;
            pName  =_pName ;
            billNo = _billNo;
        }

        private void FrmpackCount_Load(object sender, EventArgs e)
        {
            bindingSourcecustList.DataSource = custList.getcustListAll();
            comboBoxcustName.DataSource = bindingSourcecustList;
            comboBoxcustName.DisplayMember = "custName";
            comboBoxcustName.ValueMember = "custNum";
            bindingSourceselect.DataSource = packStockIn.getpackCountAll ();
            dGridslect.DataSource = bindingSourceselect;
            DcolumnSet.colSet(moduleInfo.VpackCount, dGridslect);
            comboBoxcustName.Text= GlobalInfo.address ;
            textPnameQuery.Text= GlobalInfo.custPO ;
             textBox1.Text=GlobalInfo.hetongNo ;
            textPnameQuery.Focus();
          
        }

        private void buttenter_Click(object sender, EventArgs e)
        {
            try
            {
                pname = dGridslect.CurrentRow.Cells[field].Value.ToString();
                boxnoI  = dGridslect.CurrentRow.Cells[boxno ].Value.ToString();
                MweightI  = dGridslect.CurrentRow.Cells[Mweight ].Value.ToString();
                JweightI  = dGridslect.CurrentRow.Cells[Jweight ].Value.ToString();
                qtyI = (Convert.ToDecimal(dGridslect.CurrentRow.Cells[qty].Value.ToString())).ToString("#0.0");
                unitI  = dGridslect.CurrentRow.Cells[unit ].Value.ToString();
                pNameI = dGridslect.CurrentRow.Cells[pName].Value.ToString();
               pTableI= getTable();
               GlobalInfo.address = comboBoxcustName.Text;
               GlobalInfo.custPO = textPnameQuery.Text;
               GlobalInfo.hetongNo = textBox1.Text;
            }
            catch (Exception)
            {
                MessageBox.Show("选择正确的名称");
            }
        }
        public  produceDataSet.orderStockOutDetailDataTable  getTable()
        {
            produceDataSet.orderStockOutDetailDataTable table = new produceDataSet.orderStockOutDetailDataTable();
            foreach (DataGridViewRow row in dGridslect.SelectedRows)
            {

                produceDataSet.orderStockOutDetailRow  packRow = table.NeworderStockOutDetailRow  ();
                packRow.billNo = billNo;
                packRow.orderDetailNo  = row.Cells["orderDetailNo"].Value.ToString();
               
                packRow.boxno = Convert.ToInt32(row.Cells["boxno"].Value.ToString());
                packRow.weightM  = Convert.ToDecimal(row.Cells["Mweight"].Value.ToString());
                if ((row.Cells["Jweight"].Value.ToString()) != "")
                {
                    packRow.weightJ = Convert.ToDecimal(row.Cells["Jweight"].Value.ToString());
                }
                else { packRow.weightJ = 0; }
                packRow.qty = Convert .ToDecimal ((Convert.ToDecimal(row.Cells["qty"].Value.ToString())).ToString("#0.0"));
                packRow.unit = row.Cells["unit"].Value.ToString();
                packRow.pName = row.Cells["pName"].Value.ToString();
                table.AddorderStockOutDetailRow (packRow);
            }
            return table;
        }
        private void buttexit_Click(object sender, EventArgs e)
        {
            GlobalInfo.address = comboBoxcustName.Text;
            GlobalInfo.custPO = textPnameQuery.Text;
            GlobalInfo.hetongNo = textBox1.Text;
            this.Close();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {

            bindingSourceselect.DataSource = basicSql.getVpackCount(comboBoxcustName.Text, "%" + textPnameQuery.Text + "%", "%" + textBox1.Text + "%");
            dGridslect.DataSource = bindingSourceselect;
            DcolumnSet.colSet(moduleInfo.VpackCount  , dGridslect);
            GlobalInfo.address = comboBoxcustName.Text;
            GlobalInfo.custPO = textPnameQuery.Text;
            GlobalInfo.hetongNo = textBox1.Text;
        }

        private void dGridslect_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
         
                this.buttenter.PerformClick();
            }
            catch (Exception)
            {
                MessageBox.Show("选择正确的名称");
            }
        }
        /// <summary>  
        /// 设置编辑状态下按回车键，跳到指定单元格.  
        /// </summary>  
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            string field;
            field = "matNum";
            if (keyData == Keys.Enter &&
                     (
                     (
                      !(ActiveControl is System.Windows.Forms.TextBox) ||
                      !((System.Windows.Forms.TextBox)ActiveControl).AcceptsReturn)
                     )
                     )   //监听回车事件   
            {
                //MessageBox.Show(ActiveControl.Name.ToString());
                if (ActiveControl == textPnameQuery)
                {
                    try
                    {
                        baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
                        bindingSourceselect.DataSource = q.sqlQueryLikeAll(tablename, textPnameQuery.Text.ToString().Trim());
                        dGridslect.DataSource = bindingSourceselect;
                        DcolumnSet.colSet(tablename, dGridslect);
                        dGridslect.Focus();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                if (this.dGridslect.CurrentCell.ColumnIndex == 1)
                {
                    try
                    {
                        pname = dGridslect.CurrentRow.Cells[field].Value.ToString();
                        this.DialogResult = DialogResult.Yes;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("选择正确的名称");
                    }
                }
                else
                {
                    SendKeys.Send("{Enter}");
                }

            }

            //继续原来base.ProcessCmdKey中的处理   
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void button1_Click(object sender, EventArgs e)
        {
      
        }

        private void dGridslect_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
